﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

namespace SDT.Libraries.Utilities
{
    /// <summary>
    /// Class provides methods for working with string.
    /// </summary>
    public class StringHelper
    {
        #region String methods
        #region Trim method
        /// <summary>
        /// Trim value of an object
        /// </summary>
        public static string Trim(string input)
        {
            if (input == null)
                input = "";
            return input.Trim();
        }

        public static string Trim(string input, string DelimitSign)
        {
            if (input == null)
                input = "";
            return input.Trim(DelimitSign.ToCharArray());
        }

        public static string Trim(HtmlInputText txtInput)
        {
            return txtInput.Value.Trim();
        }

        public static string Trim(HtmlInputText txtInput, string DelimitSign)
        {
            return txtInput.Value.Trim(DelimitSign.ToCharArray());
        }

        public static string Trim(TextBox txtInput)
        {
            return txtInput.Text.Trim();
        }

        public static string Trim(TextBox txtInput, string DelimitSign)
        {
            return txtInput.Text.Trim(DelimitSign.ToCharArray());
        }

        public static string Trim(DropDownList ddlInput)
        {
            return ddlInput.SelectedValue.Trim();
        }

        public static string Trim(DropDownList ddlInput, string DelimitSign)
        {
            return ddlInput.SelectedValue.Trim(DelimitSign.ToCharArray());
        }

        public static string Trim(ListBox lbInput)
        {
            if (lbInput.SelectionMode == ListSelectionMode.Multiple)
            {
                throw new Exception("Cannot process with Multiple Selection mode");
            }
            else
            {
                return lbInput.SelectedValue.Trim();
            }
        }

        public static string Trim(ListBox lbInput, string DelimitSign)
        {
            if (lbInput.SelectionMode == ListSelectionMode.Multiple)
            {
                throw new Exception("Cannot process with Multiple Selection mode");
            }
            else
            {
                return lbInput.SelectedValue.Trim(DelimitSign.ToCharArray());
            }
        }

        public static string Trim(HtmlSelect lsInput)
        {
            if (lsInput.Multiple)
            {
                throw new Exception("Cannot process with Multiple Selection mode");
            }
            else
            {
                return lsInput.Value.Trim();
            }
        }

        public static string Trim(HtmlSelect lsInput, string DelimitSign)
        {
            if (lsInput.Multiple)
            {
                throw new Exception("Cannot process with Multiple Selection mode");
            }
            else
            {
                return lsInput.Value.Trim(DelimitSign.ToCharArray());
            }
        }
        #endregion

        /// <summary>
        /// Split a string into an array of strings by a delimiter.
        /// </summary>
        public static string[] Split(string input, string delimiter)
        {
            return Regex.Split(input, Regex.Escape(delimiter));
        }

        /// <summary>
        /// Reverse a string.
        /// </summary>
        /// <param name="str">Input string.</param>
        /// <returns>Reverse string of the input.</returns>
        public static string Reverse(string str)
        {
            char[] chars = str.ToCharArray();
            Array.Reverse(chars);

            return new string(chars);
        }

        public static string LimitWords(string str, int length)
        {
            // split input string into words (max length + 1...last words go in last element)
            string[] Words = str.Split(new char[] { ' ' }, length + 1);

            // if we reach maximum words, replace last words with elipse
            if (Words.Length == length + 1)
                Words[length] = "...";
            else
                return str;  // nothing to do

            // build new output string
            string output = string.Join(" ", Words);
            return output;
        }

        private static StringReplacer whiteSpaceRemover = null;
        public static string TrimWhiteSpace(string content)
        {
            if (whiteSpaceRemover == null)
            {
                whiteSpaceRemover = new StringReplacer();
                whiteSpaceRemover.AddStringReplacementPair("\r\n", " ");
                whiteSpaceRemover.AddStringReplacementPair("\t", " ");
                whiteSpaceRemover.AddStringReplacementPair("  ", " ");
            }

            whiteSpaceRemover.SetContent(content);

            return whiteSpaceRemover.GetReplacedContent();
        }

        public static string RemoveSpecialCharacters(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return string.Empty;
            }
            if (whiteSpaceRemover == null)
            {
                whiteSpaceRemover = new StringReplacer();
                whiteSpaceRemover.AddStringReplacementPair("&", "&amp;");
                whiteSpaceRemover.AddStringReplacementPair("<", "&lt;");
                whiteSpaceRemover.AddStringReplacementPair(">", "&gt;");
                whiteSpaceRemover.AddStringReplacementPair("'", "&apos;");
                whiteSpaceRemover.AddStringReplacementPair("\"", "&quot;");
            }
            whiteSpaceRemover.SetContent(input);
            return whiteSpaceRemover.GetReplacedContent();
        }

        public static StringCollection ExtractBlocks(string content, string startTemplate, string startItemTemplate, string endItemTemplate)
        {
            StringCollection list = new StringCollection();
            int index = content.IndexOf(startTemplate);
            int num2 = 0;
            while ((index >= 0) && (num2 >= 0))
            {
                index = content.IndexOf(startItemTemplate, (int)(index + 1));
                num2 = content.IndexOf(endItemTemplate, (int)(index + startItemTemplate.Length));
                if ((index >= 0) && (num2 >= 0))
                {
                    list.Add(content.Substring(index + startItemTemplate.Length, (num2 - index) - startItemTemplate.Length));
                }
            }
            return list;
        }

        public static string GetStringByGroup(string input, string expression)
        {
            Regex regex = new Regex(expression, RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.Multiline);
            Match match = regex.Match(input);
            return match.Groups["group"].Value.Trim();
        }
        #endregion

        /***************************************************************************/

        public static Guid ToGuid( string target)
        {
            Guid result = Guid.Empty;

            if ((!string.IsNullOrEmpty(target)) && (target.Trim().Length == 22))
            {
                string encoded = string.Concat(target.Trim().Replace("-", "+").Replace("_", "/"), "==");

                try
                {
                    byte[] base64 = Convert.FromBase64String(encoded);

                    result = new Guid(base64);
                }
                catch (FormatException)
                {
                }
            }

            return result;
        }
        public static string DisplayFormatDateTimeFromOject(object d1)
        {
            DateTime d;
            try
            {
                d = Convert.ToDateTime(d1);
            }
            catch
            {

                return "";
            }
            if (d.Year != DateTime.Now.Year)
                return d.ToString("MM/dd/yyyy");
            if (d.Month != DateTime.Now.Month)
                return d.ToString("MM/dd");
            if (d.Day != DateTime.Now.Day)
                return d.ToString("MM/dd");
            return d.ToString("HH:mm");
        }
    }
}
